
{***********************************************************}
{                                                           }
{      Informix Data Access Components                      }
{                                                           }
{      TIfxConnection editor form                           }
{                                                           }
{      Copyright (c) 2001-2008 Luxena Software Company      }
{      http://www.luxena.com                                }
{      e-mail: support@luxena.com                           }
{                                                           }
{***********************************************************}

unit IfxConParamForm;

{$I lx.inc}

interface

uses
  SysUtils, Classes, Windows, Forms, Controls, StdCtrls, ComCtrls, Dialogs,
  Mask, IfxConnection;

type

{ TIfxFormConParams }

  TIfxFormConParams = class(TForm)
    OpenDialog: TOpenDialog;
    SaveDialog: TSaveDialog;
    buttonOK: TButton;
    buttonCancel: TButton;
    PageControl: TPageControl;
    tabEnvironment: TTabSheet;
    Memo: TMemo;
    buttonLoad: TButton;
    buttonSave: TButton;
    buttonDefaults: TButton;
    tabServer: TTabSheet;
    labelServerName: TLabel;
    labelHost: TLabel;
    labelProtocol: TLabel;
    labelService: TLabel;
    labelUser: TLabel;
    labelPassword: TLabel;
    labelDatabase: TLabel;
    comboServer: TComboBox;
    comboHost: TComboBox;
    comboProtocol: TComboBox;
    editPassword: TMaskEdit;
    editService: TEdit;
    editUser: TEdit;
    comboDatabase: TComboBox;
    tabAbout: TTabSheet;
    labelIDAC: TLabel;
    labelVersion: TLabel;
    labelCopyright: TLabel;
    labelSiteLink: TLabel;
    buttonTest: TButton;
    tabPooling: TTabSheet;
    checkUsePool: TCheckBox;
    boxPooling: TGroupBox;
    labelPoolMin: TLabel;
    labelPoolMax: TLabel;
    labelPoolTimeOut: TLabel;
    labelPoolLifeTime: TLabel;
    editPoolMin: TEdit;
    editPoolMax: TEdit;
    editPoolTimeOut: TEdit;
    editPoolLifeTime: TEdit;
    labelPoolMaxTime: TLabel;
    editPoolMaxTime: TEdit;

    procedure buttonTestClick(Sender: TObject);

    procedure buttonDefaultsClick(Sender: TObject);
    procedure buttonLoadClick(Sender: TObject);
    procedure buttonSaveClick(Sender: TObject);

    procedure tabServerShow(Sender: TObject);
    procedure comboServerChange(Sender: TObject);
    procedure comboHostChange(Sender: TObject);
    procedure comboProtocolChange(Sender: TObject);
    procedure editServiceChange(Sender: TObject);
    procedure editUserChange(Sender: TObject);
    procedure editPasswordChange(Sender: TObject);
    procedure comboDatabaseChange(Sender: TObject);
    procedure comboDatabaseDropDown(Sender: TObject);

    procedure checkUsePoolClick(Sender: TObject);
    procedure editPoolMinChange(Sender: TObject);
    procedure editPoolMaxChange(Sender: TObject);
    procedure editPoolTimeOutChange(Sender: TObject);
    procedure editPoolLifeTimeChange(Sender: TObject);
    procedure labelSiteLinkClick(Sender: TObject);
    procedure editPoolMaxTimeChange(Sender: TObject);

  private
    FOriginalConnection: TIfxConnection;
    FEditedConnection: TIfxConnection;
    FIsDatabasesRead: Boolean;
    function EditToCardinal(Edit: TEdit; Default: Cardinal): Cardinal;
    procedure InitServerTabControls;
    procedure UpdatePoolingBoxState;

  public
    class function Edit(Connection: TIfxConnection; const Caption: string = ''): Boolean;
    constructor Create(Connection: TIfxConnection; const Caption: string = ''); reintroduce;
    destructor Destroy; override;
    function Run: Boolean;
  end;


implementation

uses ShellAPI, LXConsts, IfxConsts;

{$R *.DFM}


{ TIfxFormConParams }

class function TIfxFormConParams.Edit(Connection: TIfxConnection;
  const Caption: string): Boolean;
begin
  with TIfxFormConParams.Create(Connection, Caption) do
    try
      Result := Run;
    finally
      Free;
    end;
end;

constructor TIfxFormConParams.Create(Connection: TIfxConnection;
  const Caption: string);
var
  OwnerName: string;
begin
  inherited Create(nil);
  if Caption <> '' then
    Self.Caption := Caption else
  begin
    if Connection.Owner <> nil then
      OwnerName := Connection.Owner.Name + '.';
    Self.Caption := Format('%s%s Params', [OwnerName, Connection.Name]);
  end;
  FOriginalConnection := Connection;
  FEditedConnection := TIfxConnection.Create(nil);
  FEditedConnection.AssignParams(Connection);
  FEditedConnection.LoginPrompt := False;
  Memo.Lines.Assign(FEditedConnection.Params);
  checkUsePool.Checked := FEditedConnection.PoolOptions.Enabled;
  UpdatePoolingBoxState;
  editPoolMin.Text := IntToStr(FEditedConnection.PoolOptions.Min);
  editPoolMax.Text := IntToStr(FEditedConnection.PoolOptions.Max);
  editPoolTimeOut.Text := IntToStr(FEditedConnection.PoolOptions.TimeOut);
  editPoolLifeTime.Text := IntToStr(FEditedConnection.PoolOptions.LifeTime);
  editPoolMaxTime.Text := IntToStr(FEditedConnection.PoolOptions.MaxTime);
  labelVersion.Caption := labelVersion.Caption + ' ' + IDACVersion;
end;

destructor TIfxFormConParams.Destroy;
begin
  FEditedConnection.Free;
  inherited;
end;

function TIfxFormConParams.Run: Boolean;
begin
  Result := ShowModal = mrOk;
  if Result then
  begin
    FEditedConnection.Params.Assign(Memo.Lines);
    FOriginalConnection.AssignParams(FEditedConnection);
  end;
end;

procedure TIfxFormConParams.buttonTestClick(Sender: TObject);
begin
  try
    FEditedConnection.Params.Assign(Memo.Lines);
    FEditedConnection.Open;
    FEditedConnection.Close;
    MessageDlg('Test connection succeeded.', mtInformation, [mbOK], 0);
  except
    on E: Exception do
      MessageDlg(E.Message, mtError, [mbOK], 0);
  end;
end;

type
  TDefaultsThread = class(TThread)
  private
    FStrings: TStrings;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Execute; override;
    property Defaults: TStrings read FStrings;
  end;

constructor TDefaultsThread.Create;
begin
  FStrings := TStringList.Create;
  inherited Create(False);
end;

destructor TDefaultsThread.Destroy;
begin
  FStrings.Free;
  inherited;
end;

procedure TDefaultsThread.Execute;
begin
  FStrings.BeginUpdate;
  try
    TIfxConnection.GetInformixEnvironment(FStrings);
  finally
    FStrings.EndUpdate;
  end;
end;

procedure TIfxFormConParams.buttonDefaultsClick(Sender: TObject);
begin
  with TDefaultsThread.Create do
    try
      WaitFor;
      Memo.Lines.Assign(Defaults);
    finally
      Free;
    end;
end;

procedure TIfxFormConParams.buttonLoadClick(Sender: TObject);
begin
  if OpenDialog.Execute then
    Memo.Lines.LoadFromFile(OpenDialog.FileName);
end;

procedure TIfxFormConParams.buttonSaveClick(Sender: TObject);
begin
  if SaveDialog.Execute then
    Memo.Lines.SaveToFile(SaveDialog.FileName);
end;

procedure TIfxFormConParams.InitServerTabControls;
begin
  TIfxConnection.GetServerNames(comboServer.Items);
  TIfxConnection.GetHostNames(comboHost.Items);
  TIfxConnection.GetProtocolNames(comboProtocol.Items);
  comboServer.Text := Memo.Lines.Values[szSERVER];
  comboHost.Text := Memo.Lines.Values[szHOST];
  comboProtocol.Text := Memo.Lines.Values[szPROTOCOL];
  if editService.Text <> Memo.Lines.Values[szSERVICE] then
    editService.Text := Memo.Lines.Values[szSERVICE];
  if editUser.Text <> Memo.Lines.Values[szUSER] then
    editUser.Text := Memo.Lines.Values[szUSER];
  if editPassword.Text <> Memo.Lines.Values[szPASSWORD] then
    editPassword.Text := Memo.Lines.Values[szPASSWORD];
  if comboDatabase.Text <> Memo.Lines.Values[szDATABASE] then
    comboDatabase.Text := Memo.Lines.Values[szDATABASE];
  FIsDatabasesRead := False;
end;

procedure TIfxFormConParams.tabServerShow(Sender: TObject);
begin
  InitServerTabControls;
end;

procedure TIfxFormConParams.comboServerChange(Sender: TObject);
begin
  Memo.Lines.Values[szSERVER] := comboServer.Text;
  comboDatabase.Items.Clear;
  FIsDatabasesRead := False;
end;

procedure TIfxFormConParams.comboHostChange(Sender: TObject);
begin
  Memo.Lines.Values[szHOST] := comboHost.Text;
end;

procedure TIfxFormConParams.comboProtocolChange(Sender: TObject);
begin
  Memo.Lines.Values[szPROTOCOL] := comboProtocol.Text;
end;

procedure TIfxFormConParams.editServiceChange(Sender: TObject);
begin
  Memo.Lines.Values[szSERVICE] := editService.Text;
end;

procedure TIfxFormConParams.editUserChange(Sender: TObject);
begin
  Memo.Lines.Values[szUSER] := editUser.Text;
end;

procedure TIfxFormConParams.editPasswordChange(Sender: TObject);
begin
  Memo.Lines.Values[szPASSWORD] := editPassword.Text;
end;

procedure TIfxFormConParams.comboDatabaseChange(Sender: TObject);
begin
  Memo.Lines.Values[szDATABASE] := comboDatabase.Text;
end;

procedure TIfxFormConParams.comboDatabaseDropDown(Sender: TObject);
begin
  if not FIsDatabasesRead then
  begin
    try
      FEditedConnection.Params.Assign(Memo.Lines);
      FEditedConnection.Params.Values[szDATABASE] := '';
      FEditedConnection.Open;
      try
        FEditedConnection.GetDatabaseNames(comboDatabase.Items);
      finally
        FEditedConnection.Close;
      end;
      FIsDatabasesRead := True;
    except
      on E: Exception do
      begin
        MessageDlg(E.Message, mtError, [mbOK], 0);
        Abort;
      end;
    end;
  end;
end;

procedure TIfxFormConParams.UpdatePoolingBoxState;
begin
  boxPooling.Enabled := checkUsePool.Checked;
  editPoolMin.Enabled := checkUsePool.Checked;
  labelPoolMin.Enabled := checkUsePool.Checked;
  editPoolMax.Enabled := checkUsePool.Checked;
  labelPoolMax.Enabled := checkUsePool.Checked;
  editPoolTimeOut.Enabled := checkUsePool.Checked;
  labelPoolTimeOut.Enabled := checkUsePool.Checked;
  editPoolLifeTime.Enabled := checkUsePool.Checked;
  labelPoolLifeTime.Enabled := checkUsePool.Checked;
  editPoolMaxTime.Enabled := checkUsePool.Checked;
  labelPoolMaxTime.Enabled := checkUsePool.Checked;
end;

function TIfxFormConParams.EditToCardinal(Edit: TEdit; Default: Cardinal): Cardinal;
var
  Value: Integer;
  SelStart: Integer;

  function MakeDefault: Cardinal;
  begin
    SelStart := Edit.SelStart;
    Edit.Text := IntToStr(Default);
    Edit.SelStart := SelStart - 1;
    Result := Default;
  end;

begin
  if Edit.Text = '' then
  begin
    Edit.Text := '0';
    Result := 0;
  end else
    try
      Value := StrToInt(Edit.Text);
      if Value < 0 then
        Result := MakeDefault else
        Result := Value;
    except
      Result := MakeDefault;
    end;
end;

procedure TIfxFormConParams.checkUsePoolClick(Sender: TObject);
begin
  FEditedConnection.PoolOptions.Enabled := checkUsePool.Checked;
  UpdatePoolingBoxState;
end;

procedure TIfxFormConParams.editPoolMinChange(Sender: TObject);
begin
  FEditedConnection.PoolOptions.Min := EditToCardinal(editPoolMin,
    FEditedConnection.PoolOptions.Min);
end;

procedure TIfxFormConParams.editPoolMaxChange(Sender: TObject);
begin
  FEditedConnection.PoolOptions.Max := EditToCardinal(editPoolMax,
    FEditedConnection.PoolOptions.Max);
end;

procedure TIfxFormConParams.editPoolTimeOutChange(Sender: TObject);
begin
  FEditedConnection.PoolOptions.TimeOut := EditToCardinal(editPoolTimeOut,
    FEditedConnection.PoolOptions.TimeOut);
end;

procedure TIfxFormConParams.editPoolLifeTimeChange(Sender: TObject);
begin
  FEditedConnection.PoolOptions.LifeTime := EditToCardinal(editPoolLifeTime,
    FEditedConnection.PoolOptions.LifeTime);
end;

procedure TIfxFormConParams.editPoolMaxTimeChange(Sender: TObject);
begin
  FEditedConnection.PoolOptions.MaxTime := EditToCardinal(editPoolMaxTime,
    FEditedConnection.PoolOptions.MaxTime);
end;

procedure TIfxFormConParams.labelSiteLinkClick(Sender: TObject);
begin
  ShellExecute(0, 'open', SCompanyURL, nil, nil, SW_SHOWNORMAL);
end;


end.